package com.store.app.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.easeui.model.DBHelper_hy;
import com.hyphenate.easeui.model.recordBean_hy;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.BookedViewPagerAdapter;
import com.store.app.bean.CouponUpdateBean;
import com.store.app.fragment.TheOrderHPTFragment;
import com.store.app.fragment.TheOrderHuoPinTuanFragment;
import com.store.app.fragment.TheOrderSellFragment;
import com.store.app.imlife.activity.ChatActivity;
import com.store.app.imlife.utils.ToastTools;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 卖单页面
 */
public class TheSellOrderActivity extends FragmentActivity implements View.OnClickListener {
    public List<Fragment> fragments = new ArrayList<Fragment>();
    private TextView radioGroup_titlebar_1, radioGroup_titlebar_4, booked_hpt;
    private TextView title;
    private ViewPager viewpager;
    private String showFragmentIndex = "0";
    private TextView tv1;
    private TextView tv4;
    private TextView tvSellNumber;

    private View tv_Right1;
    //数据库存储
    private DBHelper_hy db = null;
    private recordBean_hy rb = null;

    private TextView txt_hpt;//火拼团
    private EditText et_search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_booked_sell);
        initView();
        findView();
        setListener();
        initData();
        if (db == null) {
            db = new DBHelper_hy(this);
            rb = new recordBean_hy();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void setListener() {
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        /**
         *
         */

        //跳到店东助手官方客服
        tv_Right1.setVisibility(View.VISIBLE);
        tv_Right1.setBackgroundResource(R.drawable.pic_guanfangkefu);
        tv_Right1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String chatid = "diandongzhushouserviceagent";
                String title = "官方客服";
                Intent intent = new Intent(TheSellOrderActivity.this, ChatActivity.class);
                intent.putExtra("userId", chatid);
                intent.putExtra("groupName", title);
                startActivity(intent);

                try {
                    rb.saveMember(db, "", chatid, title
                            , "", "https://oss-img.meitianhui.com/communityDocs/kefu.jpg", "0");
                } catch (Exception e) {
                }

            }
        });

    }

    private void findView() {
//		radioGroup = (RadioGroup) findViewById(R.id.booked_rg);
//		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//
//			@Override
//			public void onCheckedChanged(RadioGroup group, int checkedId) {
//				switch (checkedId) {
//					case R.id.booked_rb1:
//						viewpager.setCurrentItem(0);
//						break;
//					case R.id.booked_rb2:
//						viewpager.setCurrentItem(1);
//						break;
//					case R.id.booked_rb3:
//						viewpager.setCurrentItem(2);
//						break;
//					default:
//						break;
//				}
//
//			}
//		});

        viewpager = (ViewPager) findViewById(R.id.booked_viewpager);
        radioGroup_titlebar_1 = (TextView) findViewById(R.id.booked_rb1_titleck1);
        radioGroup_titlebar_4 = (TextView) findViewById(R.id.booked_rb4_titleck4);
        booked_hpt = (TextView) findViewById(R.id.booked_hpt);

        tv_Right1 = findViewById(R.id.tv_right1);
        tv_Right1.setVisibility(View.VISIBLE);
        tv_Right1.setBackgroundResource(R.drawable.backfankui);

        tv1 = (TextView) findViewById(R.id.tv1);
        tv4 = (TextView) findViewById(R.id.tv4);
        txt_hpt = (TextView) findViewById(R.id.txt_hpt);

        tvSellNumber = (TextView) findViewById(R.id.tv_sell_number);
        title = (TextView) findViewById(R.id.tvTitle);
        title.setText("卖单");
        tv1.setOnClickListener(this);
        tv4.setOnClickListener(this);
        txt_hpt.setOnClickListener(this);
        if (tvSellNumber.getText().toString().trim().equals("0")) {
            tvSellNumber.setVisibility(View.GONE);
        }
    }

    private void refreshUITab(String index) {
        if (index.equals("0")) {
            tv1.setTextColor(Color.parseColor("#f96116"));
            tv4.setTextColor(Color.parseColor("#333333"));
            txt_hpt.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_1.setBackgroundColor(getResources().getColor(R.color.booked_color));
            radioGroup_titlebar_4.setBackgroundColor(getResources().getColor(R.color.transparent));
            booked_hpt.setBackgroundColor(getResources().getColor(R.color.transparent));
        } else if (index.equals("1")) {
            tv1.setTextColor(Color.parseColor("#333333"));
            tv4.setTextColor(Color.parseColor("#f96116"));
            txt_hpt.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_1.setBackgroundColor(getResources().getColor(R.color.transparent));
            radioGroup_titlebar_4.setBackgroundColor(getResources().getColor(R.color.booked_color));
            booked_hpt.setBackgroundColor(getResources().getColor(R.color.transparent));
        } else if (index.equals("2")) {
            tv1.setTextColor(Color.parseColor("#333333"));
            tv4.setTextColor(Color.parseColor("#333333"));
            txt_hpt.setTextColor(Color.parseColor("#f96116"));
            radioGroup_titlebar_1.setBackgroundColor(getResources().getColor(R.color.transparent));
            radioGroup_titlebar_4.setBackgroundColor(getResources().getColor(R.color.transparent));
            booked_hpt.setBackgroundColor(getResources().getColor(R.color.booked_color));
        }
    }

    private void initData() {
        fragments.add(new TheOrderSellFragment());
        fragments.add(new TheOrderHPTFragment());
        fragments.add(new TheOrderHuoPinTuanFragment());
        BookedViewPagerAdapter disfratab = new BookedViewPagerAdapter(getSupportFragmentManager(), viewpager, fragments);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refreshUITab(position + "");
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        refreshUITab(showFragmentIndex);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv1:
                showFragmentIndex = "0";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(0);
                break;
            case R.id.tv4:
                showFragmentIndex = "1";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(1);
                break;
            case R.id.txt_hpt://火拼团
                showFragmentIndex = "2";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(2);
                break;
        }
    }

    public void onEvent(CouponUpdateBean event) {
        Log.v("zyl", "onEvent");
        //卖单数据修改
        if (event.type == 99) {
            if (event.number.equals("0")) {
                tvSellNumber.setVisibility(View.GONE);
            } else {
                tvSellNumber.setVisibility(View.VISIBLE);
                tvSellNumber.setText(event.number);
            }
        }
    }

    private void initView() {
        et_search = (EditText) findViewById(R.id.et_search);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String content = et_search.getText().toString().trim() + "";
                    if(!TextUtils.isEmpty(content)){
                        Intent intent = new Intent(TheSellOrderActivity.this,SearchOrderActivity.class);
                        intent.putExtra("loaded_code",content);
                        startActivity(intent);
                        try {
                            // 隐藏软键盘
                            InputMethodManager imm = (InputMethodManager) TheSellOrderActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(TheSellOrderActivity.this.getWindow().getDecorView().getWindowToken(), 0);
                        }catch (Exception e){e.printStackTrace();}
                    }else{
                        ToastTools.showShort(TheSellOrderActivity.this,"请输入提货码!");
                    }

                }
                return false;
            }
        });
    }

    private void submit() {
        // validate
        String search = et_search.getText().toString().trim();
        if (TextUtils.isEmpty(search)) {
            Toast.makeText(this, "请输入提货码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}